<?php

namespace brand\components;

use brand\logic\Config;
use brand\page\Form;
use brand\page\Table;
use brand\traits\Component as _Component;

/**
 * @method static Button  Button()
 */
class Component
{
    /**
     * 表单对象
     */
    public $form;

    /**
     * 表格对象
     */
    public $table;

    public static $SurfaceTable;
    public static $SurfaceForm;

    /**
     * 数据表索引
     */
    public $pk;

    /**
     * 当前字段
     */
    public $field;

    /**
     * 当前字段数据库配置
     */
    public $label;

    /**
     * 是否是搜索页面
     */
    public $is_search = 0;

    /**
     * 当前表格值
     */
    protected $value;
    /**
     * 当前curd配置信息
     */
    protected $config;

    /**
     * 表单验证信息
     */
    protected $validate;

    /**
     * 表单备注信息
     */
    protected $marker;

    /**
     * Props
     */
    protected $props;
    protected $visible;

    /**
     * 表单数据
     */
    public $raws;

    /**
     * 表单选择数据
     */
    protected $options = [];

    /**
     * 文件上传url
     */
    public $upload_url;

    /**
     * 资料库url
     */
    public $manage_url;

    public $component;

    public $form_column;


    use _Component;

    /**
     * @param $config array curd配置信息
     * @param $raws array 表单信息
     */
    public function __construct(array $config = [], array $raws = [])
    {
        //CURD配置
        $this->config = $config;
        //总数据
        $this->raws = $raws;
        //字段名称
        $this->label = $config['title'] ?? "";

        $this->upload_url = Config::getUploadUrl();
        $this->manage_url = Config::getManageUrl();

        $this->_init();
        //构建Props
        $this->buildProps();

        $this->form = new Form();



    }

    public static function __callStatic($name, $arguments)
    {
        return self::make($name, $arguments);
    }


    public static function make($name, $param = [])
    {

        $class = Config::BRAND_NAMESPACE . ucfirst(strtolower($name));
        if (class_exists($class)) {
            $param[] = $param[0] ?? [];
            $param[] = $param[1] ?? [];
            return (new \ReflectionClass($class))->newInstanceArgs($param);
        }

    }

    //构建
    public function buildColumns()
    {
        //字段内容
        $this->value = $this->raws[$this->field] ?? ($this->config['default'] ?? "");
        //构建验证器
        $this->buildVilidate();
        //验证字段
        $this->checkFormValue();
    }

    /**
     * 验证表格字段是否存在
     * @param $key
     */
    public function issetTableValue($key)
    {
        if(isset($this->config['table'][$key]) && $this->config['table'][$key]){
            return $this->config['table'][$key];
        }else{
            return false;
        }
    }
    /**
     * 验证表单字段是否存在
     * @param $key
     */
    public function issetFormValue($key)
    {
        if(isset($this->config['form'][$key]) && $this->config['form'][$key]){
            return $this->config['form'][$key];
        }else{
            return false;
        }
    }

    public function setFormParam($name)
    {
        return $this->getConfigParam('form',$name);
    }

    public function getFormParam($name)
    {
        return $this->getConfigParam('form',$name);
    }

    public function getTableParam($name)
    {
        return $this->getConfigParam('table',$name);
    }

    public function getConfigParam($method,$name)
    {
        $config = $this->config[$method] ?? [];
        $name = explode('.',$name);
        foreach ($name as $item) {
            $config = $config[$item] ?? null;
        }
        return $config;
    }


    public function buildSelectConfig($tableConfigValue,$thisMethod,$curd_method = 'curd_method'): array
    {
        $options['value'] = \brand\Brand::Builder('键')->build();
        $options['label'] = \brand\Brand::Builder('描述')->build();
        $cloumn = \brand\Brand::Builder('选项配置','arrays')
            ->form_options([$options])
            ->form_props([
                'append'    =>  true,
                'span'  =>  12,
                'title' => false
            ])
            ->form_visible([['prop' => $curd_method, 'value' => $thisMethod]])
            ->build();
        $values = $tableConfigValue['form']['options'] ?? [];

        return ['column' => $cloumn,'value' => $values];
    }









}